var  app = getApp();
const log =require('../../log.js');
const util=require('../../util.js');
let keys = '4UTBZ-PUWLX-UGS4R-TIK3C-WFL73-N4FWH';
const tabService = require("../../libs/tabService");
const QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
const qqmapsdk = new QQMapWX({
  key:keys
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
      itemIndex:0,
      margin:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that=this;   
        console.log('-------detail --onload-options----')
        console.log(options);
        console.log('-------detail --onload-options----')
        var parent_id=options.parent_id;
        var store_id=options.id;
        var sn=options.sn;
        if(store_id!=undefined){
          console.log('store_id1=>'+store_id)
          this.setData({
            store_id:store_id
          });
          wx.setStorageSync('store_id',store_id);
      }else{
        store_id=options.store_id;
        if(store_id==undefined){
            store_id=wx.getStorageSync('store_id');
        }
        wx.setStorageSync('store_id',store_id);
        console.log('store_id2=>'+store_id)
            this.setData({
              store_id:store_id
            });
        
        

        
      }
        if(sn!=undefined){
            this.setData({
                sn:sn
            });
            wx.setStorageSync('sn',sn);
        }
        if(parent_id!=undefined){
            this.setData({
                parent_id:parent_id
              })
              wx.setStorageSync('parent_id',parent_id);
        }else{
            parent_id = wx.getStorageSync('parent_id');
        
        }
         var open=wx.getStorageSync('open'); 

        console.log('open=>'+open);
        var date=util.formatTime(new Date());
        log.info("Time:"+date+",page:index,options:"+JSON.stringify(options));
        log.info("Time:"+date+",page:index,store_id:"+store_id);
        //open="";
        //open="";
        if(open==""){
            wx.navigateTo({
              url: '../login/index?parent_id='+parent_id+'&store_id='+store_id,
            })
        }
        wx.showShareMenu({
            menus: ['shareAppMessage'],
            success(res) {
             console.log('success')
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })    
     wx.hideShareMenu({
         menus:['shareTimeline'],
         success(res) {
            console.log('success')
             console.log(res)
           },
     })
        this.getDetail(store_id);
        that.setData({
            platform:app.globalData.platform,
            city:app.globalData.city,
             open:open,
            width:app.globalData.windowWidth,
            height:app.globalData.windowHeight,
            statusBarHeight:app.globalData.statusBarHeight,
            navHeight:app.globalData.navHeight,
            marginTop:app.globalData.navTitleTop,
            domain:app.globalData.platUrl,
            uploadUrl:app.globalData.uploadUrl,
            imgUrls:app.globalData.imgUrls
          })
    },
    goIndex(){
        wx.setStorageSync('store_id',0);
        wx.reLaunch({
          url: '../index/index',
        })
    },
    imageLoad(e){
      var width=this.data.width+2;
      console.log(e);
      var h=(width/e.detail.width)*e.detail.height-1;
      var height=h-16;
      var id=e.currentTarget.dataset.id;
      var w = width*0.8;
      var margin=this.data.margin;
      margin[id]=h;
      console.log(h);
      this.setData({
         margin:margin,
          coverMarginTop:h,
          coverHeight:height,
          coverWidth:w
      })
     // console.log(this.data.width);
  },
    selectItem(e){
      var itemIndex=e.currentTarget.dataset.index;
      var item=e.currentTarget.dataset.item;
      var modelIdArray=this.data.data.modelIdArray;
      this.setData({
        rightId:'item_'+itemIndex,
        itemIndex:itemIndex
      })
      var modelId=modelIdArray[itemIndex];
      this.getModel(modelId)
    },
    getModel(modelId){
      console.log('modelId=>'+modelId);
      let that=this;
      var data=this.data.data;
      wx.request({
        url: app.globalData.platUrl+'store/getModelDetail',
        data:{id:modelId},
        success:res=>{
          console.log(res);
          data['storeModel']=res.data;
          that.setData({
            data:data
          })
        }
      })
      //console.log(id)
    },
    gotoNavigate:function(e){
      console.log(e);
          
      var latitude=e.currentTarget.dataset.lat;
      var longitude=e.currentTarget.dataset.lng;       
     var position=e.currentTarget.dataset.name;
     var address=e.currentTarget.dataset.address;
    
     wx.navigateTo({
       url: '../map/navigation?latitude='+latitude+'&longitude='+longitude+'&position='+position+'&address='+address,
     })
     
    },
    getDetail:function (id) {
        let that=this;
        wx.request({
          url: app.globalData.platUrl+'store/getDetail',
          data:{id:id},
          success:res=>{
              console.log(res.data);
              var data=res.data;
              var station=data.station;
              var phone=data.phone;        
              var lat=wx.getStorageSync('now_lat');
              var lng=wx.getStorageSync('now_lng');
              console.log(lat);
              if(lat){
                //console.log(lat);
                //that.getDistance(lat,lng,data.lat,data.lng);
              }
            
            that.setData({
                shareTitle:data.shareTitle,
                shareLogo:data.shareLogo,
                shareIndex:data.shareIndex,   
              data:data,
              phone:phone,
              brands:data.brands
            })
            }
        })
    },
    getDistance:function(latitude1,longitude1,latitude2,longitude2){
      let that=this;
      console.log('latitude1:'+latitude1+' longitude1:'+longitude1+' latitude2:'+latitude2+' longitude2:'+longitude2);
      qqmapsdk.calculateDistance({
        mode:'driving',
        from:{
          latitude:latitude1,
          longitude:longitude1
        },
        to:[{
          latitude:latitude2, 
          longitude:longitude2
        }],
        success:function(res,data){
            console.log(data);
            var distance=data.distance[0]/1000;
            that.setData({
              distance:distance
            })
        }
      })
    },
    goBack(){
        //wx.navigateBack();
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];   //当前页面
        var prevPage = pages[pages.length - 2];  //上一个页面
       // prevPage.onRefresh()//刷新方法
        wx.navigateBack({
            delta:1
        });
      },
      call(e){
        var phone=e.currentTarget.dataset.phone;
        wx.makePhoneCall({
          phoneNumber: phone,
        })
      },
      goto:function(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        var parent_id=wx.getStorageSync('parent_id');
        var index=e.currentTarget.dataset.index;
        if(url=="#"){

        }else if(url=='phone'){
            wx.makePhoneCall({
              phoneNumber: this.data.store.phone,
            })
        }else if(url=='coupon'){
            var date=util.formatTime(new Date());
            var face_value=e.currentTarget.dataset.face_value;
            var pay_money=e.currentTarget.dataset.pay_money;
            var yxq=e.currentTarget.dataset.yxq;
            var month=e.currentTarget.dataset.month;
             log.info("Time:"+date+",page:index,function:goto:,index"+index);
            this.getCoupon(face_value,month,yxq,pay_money);
        }else{
            console.log(url);
            if(index=='index'){
                wx.reLaunch({
                    url: url,
                  })
            }else{
                console.log('-----'+url);
                wx.navigateTo({
                    url: url,
                  })
            }
       
        }
      // console.log(url);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      
        var role = wx.getStorageSync('role'); 
        var  tab = wx.getStorageSync('tabBar');  
        tabService.updateTabArray(this,tab);   
        this.update(role)    
        tabService.updateIndex(this, 0);
      },
      update(role) {
        tabService.updateRole(this, role);
      },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
    onShareTimeline(){
        //console.log(e);
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var open=wx.getStorageSync('open');        
        var parent_id=open.user_id;
        var date=new Date();
        var time=date.getTime();
        var page='Index';
        var time=date.getTime();
        var second=date.getSeconds();
        var hour=date.getHours();
        var day=date.getDate();
        var store_id=wx.getStorageSync('store_id');
        var md5=day+'I'+hour+'nd'+second+'ex';
         this.setData({
           share:true
         });
         console.log('onShareTimeLine');
         console.log(shareLogo);
         console.log(title);
         wx.setStorageSync("tabIndex",0)
         var shareObj = {
            　　　　title: title,    
            　　　　path: '/pages/index/index?store_id='+store_id+'&parent_id='+parent_id,   
            　　　　imageUrl: shareLogo 
            　　　
            　　}
        console.log(shareObj);
            　　// 返回shareObj
            　　return shareObj;
       },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        var open=wx.getStorageSync('open');
        var parent_id=open.user_id;
        var that = this;
        var title=this.data.shareTitle;
        var shareLogo=this.data.shareLogo;
        var shareIndex=this.data.shareIndex;
        var store_id=wx.getStorageSync('store_id');
        wx.setStorageSync("tabIndex",1)
   　　var shareObj = {
   　　　　title: title,    
   　　　　path: '/pages/index/index?store_id='+store_id+'&parent_id='+parent_id,   
   　　　　imageUrl: shareIndex
   　　　
   　　}
   　
   console.log(shareObj);
   　　// 返回shareObj
   　　return shareObj;
    }
})